package com.itheima.jdkproxy;


import com.itheima.logdemo.Demo01;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
/*
动态代理的作用:
在不修改源代码的情况下
对方法进行修改
 */
public class Client {

    private static final Logger LOGGER= LoggerFactory.getLogger(Client.class);
    public static void main(String[] args) {

        // jdk动态代理测试
   /*     RealSubject realSubject = new RealSubject();
        realSubject.doSomething();*/


        RealSubject obj= new RealSubject();
       Subject o = (Subject) Proxy.newProxyInstance(obj.getClass().getClassLoader(),
               obj.getClass().getInterfaces(),
               new InvocationHandler() {
                   @Override
                   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                       LOGGER.debug(method.getName()+"准备执行了,参数是"+ Arrays.toString(args));
                       Object invoke = method.invoke(obj, args);
                      LOGGER.debug("方法执行结束了, 返回值是"+invoke);
                       return invoke;
                   }
               }

       );
        o.doSomething();
   //使用 自己手写的代理类创建对象 获取代理对象 代理对象调用方法
    /* Subject subject = new JDKDynamicProxy(new RealSubject()).getProxy();
        subject.doSomething();*/

    }
}